package com.fishingwithme.android.ui.screens.map.handlers

import android.graphics.BitmapFactory
import com.fishingwithme.android.R
import com.fishingwithme.android.data.model.Line

class DrawNavigationLineHandler : BaseHandler() {
    override val key: String = "drawNavigationLine"
    override val icon: Int = R.mipmap.navi// 使用"navigation"图标表示导航线绘制
    override var initializeData = true
    override var showMenu = false
    override fun doAction() {
        try {
            val line = data?.get("line") as Line

            val spotLatLng = com.tencent.tencentmap.mapsdk.maps.model.LatLng(line.latitude, line.longitude)
            // 创建MarkerOptions并设置基本属性
            val markerOptions = com.tencent.tencentmap.mapsdk.maps.model.MarkerOptions(spotLatLng)
                .title("钓点")
                .infoWindowEnable(true)
            // 创建BitmapFactory.Options以设置缩放参数
            val options = BitmapFactory.Options()
            // 设置缩放比例 (2表示1/2大小，4表示1/4大小)
            options.inSampleSize = 4

            // 从资源中加载并缩放位图
            val bitmap = BitmapFactory.decodeResource(
                context?.resources,
                com.fishingwithme.android.R.mipmap.go_fishing,
                options
            )

            // 将缩放后的位图转换为BitmapDescriptor
            val bitmapDescriptor = com.tencent.tencentmap.mapsdk.maps.model.BitmapDescriptorFactory.fromBitmap(bitmap)

            // 设置图标
            markerOptions.icon(bitmapDescriptor)

            // 添加标记到地图
            val marker = map?.addMarker(markerOptions)
            marker?.showInfoWindow()
            marker?.let {
                addMarkerToMap(it)
            }

            val points = line.line.map { point -> com.tencent.tencentmap.mapsdk.maps.model.LatLng(point.latitude, point.longitude) }
            if (points.size > 1) {
//                val polylineOptions = com.tencent.tencentmap.mapsdk.maps.model.PolylineOptions()
//                    .latLngs(points)
//                    .color(0xff00ff00.toInt()) // 绿色
//                    .width(10f)

                val polylineOptions = com.tencent.tencentmap.mapsdk.maps.model.PolylineOptions()
                polylineOptions.latLngs(points)
                    .lineCap(true)
                    // 折线的颜色为绿色
                    .color(0xff00ff00.toInt())
                    // 折线宽度为25像素
                    .width(25F)
                    // 还可以添加描边颜色
                    .borderColor(0xffff0000.toInt())
                    // 描边颜色的宽度，线宽还是 25 像素，不过填充的部分宽度为 `width` - 2 * `borderWidth`
                    .borderWidth(5F)
               val polyline=  map?.addPolyline(polylineOptions)
                if(polyline != null) {
                    addPolylineToMap(polyline)
                }

                val parkingOptions = com.tencent.tencentmap.mapsdk.maps.model.MarkerOptions(points[points.size - 1])
                    .title("停车点")
                    .infoWindowEnable(true)
                val parkingMarker = map?.addMarker(parkingOptions)
                parkingMarker?.showInfoWindow()
                parkingMarker?.let {
                    addMarkerToMap(it)
                }
            }

            // 将地图中心点移动到钓点位置并设置合适的缩放级别
            try {
                map?.moveCamera(com.tencent.tencentmap.mapsdk.maps.CameraUpdateFactory.newLatLngZoom(spotLatLng, 12f))
            } catch (e: Exception) {
                android.util.Log.w("DrawNavigationLine", "Error moving camera", e)
            }

        } catch (e: Exception) {
            // 处理异常，确保应用不会崩溃
            android.util.Log.e("MapWrapperControllerImpl", "Error showing spot on map", e)
        }

    }
}